module neo.tetris.view {
	/**
	 *
	 * @author 
	 *
	 */
	export class BattleView extends eui.Component {
        public mainBoard: TetrisViewBoard;
        
        public otherPlayerBoard0: TetrisViewBoard;
    	
		public constructor() {
            super();
            
            this.addEventListener(eui.UIEvent.COMPLETE,this.handleEvent,this);
            this.addEventListener(egret.Event.ADDED_TO_STAGE,this.handleEvent,this);
            this.addEventListener(egret.Event.REMOVED_FROM_STAGE,this.handleEvent,this);
            this.skinName = "resource/custom_skins/BattleViewSkin.exml";
        }
        
        public difficultTo(source:eui.Component, target:eui.Component) {
            var star: egret.Shape = new egret.Shape();
            star.graphics.lineStyle(2,0xffffff, .9);
            star.graphics.beginFill(0,.0);
            star.graphics.drawRect(0,0,40,40);
            star.graphics.endFill();
            this.addChild(star);
            star.x = source.x + source.width >> 1;
            star.y = source.y + source.height >> 1;
            egret.Tween.get(star).to({ x: target.x + 20 + target.width >> 1, y: target.y + 20 + target.height >> 1, rotation: 2},300,egret.Ease.sineIn)
                .wait(200).call(function() {star.parent.removeChild(star)});
        }

        private handleEvent(evt: egret.Event): void {
            switch(evt.type) {
                case egret.Event.ADDED_TO_STAGE:
                    neo.tetris.model.appInstance.addEventListener(neo.typescript.model.Bean.PROPERTY_CHANGE,this.handleModelChanged,this);
                    this.handleModelChanged();
                    break;
                case egret.Event.REMOVED_FROM_STAGE:
                    neo.tetris.model.appInstance.removeEventListener(neo.typescript.model.Bean.PROPERTY_CHANGE,this.handleModelChanged,this);
                    break;
                case eui.UIEvent.COMPLETE:
                    this.mainBoard.battleView = this.otherPlayerBoard0.battleView = this;
                    break;
            }
        }

        private handleModelChanged(evt:egret.Event = null): void {
            if (!evt || evt.data == neo.tetris.model.App.BATTLE) {
                // check wether in battle
                if (neo.tetris.model.appInstance.battle) {
                    neo.tetris.model.appInstance.battle.addEventListener(neo.typescript.model.Bean.PROPERTY_CHANGE,this.handleBattleEvt,this);
                    this.handleBattleEvt();
                }
            }
        }
        
        private handleBattleEvt(evt:egret.Event = null):void {
            if(!evt || evt.data == neo.tetris.model.BattleModel.START_TIME) {
                for(var i: number = 0;i < neo.tetris.model.appInstance.battle.players.length;i++) {
                    var player: neo.tetris.model.BattlePlayerModel = neo.tetris.model.appInstance.battle.players[i];
                    if(player.userUUid == neo.tetris.model.appInstance.logined.uuid) {
                        this.mainBoard.battlePlayer = player;
                    } else {
                        this.otherPlayerBoard0.battlePlayer = player;
                    }
                }
            }
        }
	}
}
